package com.benzhuo.widget.research.recycler;

import android.app.Activity;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.benzhuo.widget.research.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class RecyclerViewAct extends Activity {
    MyAdapter adapter;
    @BindView(R.id.vRecycler)
    ZRefreshRecyclerView vRecycler;

    int index = 0;
    @BindView(R.id.vShowRecyclerList)
    Button vShowRecyclerList;
    @BindView(R.id.vShowRecyclerGrid)
    Button vShowRecyclerGrid;
    @BindView(R.id.vShowRecyclerStaggeredGrid)
    Button vShowRecyclerStaggeredGrid;
//    @BindView(R.id.vSwipeRefresh)
//    SwipeRefreshLayout vSwipeRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        ButterKnife.bind(this);
        adapter = new MyAdapter(createStringList(8));
        vRecycler.setAdapter(adapter);
//        ZGifLoadMoreView loadMore = new ZGifLoadMoreView(this); //LayoutInflater.from(this).inflate(R.layout.load_more,null);
//        loadMore.setText("正在加载");
//        loadMore.setBitmapResource(R.mipmap.load_more);
//        vRecycler.setLoadMoreView(loadMore);
        vRecycler.setLoadMoreListener(new ZLoadRecyclerView.ILoadMoreListener() {
            @Override
            public void loadMore(ZLoadRecyclerView recyclerView) {
                Observable.just(3).subscribeOn(Schedulers.io()).delay(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        List<String> list = createStringList((int) (Math.random() * 20));
                        int start = adapter.getItemCount();
//                        adapter.addItems(list);
//                        adapter.notifyItemRangeInserted(start,list.size());
                        vRecycler.loadNoMoreData();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        vRecycler.loadFail();
                    }
                });
            }
        });

        vRecycler.setRefreshListener(new ZRefreshRecyclerView.IRefreshListener() {
            @Override
            public void refreshData(ZRefreshRecyclerView recyclerView) {
                Observable.just(3).subscribeOn(Schedulers.io()).delay(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        List<String> list = createStringList((int) (Math.random() * 20));
                        adapter.setItems(list);
                        adapter.notifyDataSetChanged();
                        vRecycler.refreshFinish();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        vRecycler.refreshFinish();
                    }
                });
            }
        });

//        vSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                Observable.just(3).subscribeOn(Schedulers.io()).delay(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
//                    @Override
//                    public void call(Integer integer) {
//                        List<String> list = createStringList((int) (Math.random() * 20));
//                        adapter.setItems(new ArrayList<String>());
//                        adapter.notifyDataSetChanged();
//                        vSwipeRefresh.setRefreshing(false);
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        vSwipeRefresh.setRefreshing(false);
//                    }
//                });
//            }
//        });
    }

    public void setColumn(int column) {
        if (column == 0) {
            vRecycler.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
        } else if (column == 1) {
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
            vRecycler.setLayoutManager(mLayoutManager);
        } else {
            GridLayoutManager mLayoutManager = new GridLayoutManager(this, column);
            vRecycler.setLayoutManager(mLayoutManager);
        }
    }

    List<String> createStringList(int number) {
        List<String> str = new ArrayList<>(8);
        for (int i = 0; i < number; i++) {
            str.add("temp" + i);
        }
        return str;
    }

    @OnClick({R.id.vShowRecyclerList, R.id.vShowRecyclerGrid, R.id.vShowRecyclerStaggeredGrid})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vShowRecyclerList:
                setColumn(1);
                break;
            case R.id.vShowRecyclerGrid:
                setColumn(3);
                break;
            case R.id.vShowRecyclerStaggeredGrid:
                setColumn(0);
                break;
        }
    }
}
